import 'package:base_lib/app.dart';
import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/parameters.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:base_lib/utils/util.dart';
import 'package:doctor_login_lib/component.dart';
import 'package:doctor_user_lib/setting/setting_bloc.dart';
import 'package:doctor_user_lib/setting/setting_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SettingPage extends BaseBlocPage<SettingBloc, _SettingState> {
  SettingPage(Parameters parameters) : super(parameters);

  @override
  _SettingState generateThemeState() {
    return _SettingState();
  }
}

class _SettingState extends BaseBlocPageState<SettingBloc> {
  void initState() {
    super.initState();
  }

  @override
  ignoreWindowWidget() => false;

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        title: Text("设置"),
      ),
      body: StreamBuilder(
        stream: bloc.settingSubject.stream,
        initialData: null,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData)
            return Container(
              child: Container(
                color: theme.themeColor.themeBgColor,
                padding: EdgeInsets.only(top: 20),
                child: ListView.separated(
                  itemCount: bloc.settingList.length + 1,
                  itemBuilder: (BuildContext context, int index) {
                    return index == bloc.settingList.length
                        ? _exitLoginButtonBuilder()
                        : _cellBuilder(bloc.settingList[index], theme, context);
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(
                      height: 1,
                      color: theme.themeColor.themeBgColor,
                    );
                  },
                ),
              ),
            );
          else
            return buildLoadingIndicator();
        },
      ),
    );
  }

  /// 退出登录按钮
  Widget _exitLoginButtonBuilder() {
    return Padding(
      padding: EdgeInsets.only(top: 20),
      child: FlatButton(
        onPressed: _showCupertinoAlertDialog,
        padding: EdgeInsets.only(top: 20, bottom: 20),
        color: Colors.white,
        child: Text(
          "退出登录",
          style: TextStyle(color: Colors.red),
        ),
      ),
    );
  }

  /// cell
  Widget _cellBuilder(SettingEntity model, MTheme theme, BuildContext context) {
    return GestureDetector(
      onTap: () {
        _cellTapGesture(model, context);
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 16, right: 16, top: 20, bottom: 20),
        child: Row(
          children: [
            Expanded(
                flex: 1,
                child: Container(
                  padding: EdgeInsets.only(right: 16),
                  child: Row(
                    children: [
                      Expanded(child: Text(model.title), flex: 1),
                      Text(model.detail,
                          style: TextStyle(
                              color: theme.themeColor.themeTextDetailColor))
                    ],
                  ),
                )),
            Offstage(
              offstage: !model.isArrows,
              child: Icon(
                Icons.arrow_forward_ios,
                color: Colors.black26,
                size: 16,
              ),
            )
          ],
        ),
      ),
    );
  }

  //
  void _showCupertinoAlertDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          title: Text("退出登录"),
          content: Column(
            children: <Widget>[
              SizedBox(
                height: 10,
              ),
              Align(
                child: Text("你确定要退出登录吗？"),
                alignment: Alignment(0, 0),
              ),
            ],
          ),
          actions: <Widget>[
            CupertinoDialogAction(
              child: Text("取消"),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            CupertinoDialogAction(
              child: Text("确定"),
              onPressed: () {
                bloc.logOut(context);
              },
            ),
          ],
        );
      },
    );
  }

  void _cellTapGesture(SettingEntity entity, BuildContext context) {
    switch (entity.title) {
      case "个人资料":
        Routers.navigateTo(context, DoctorLoginComponent.setDoctorInfoPage);
        break;
      case "注册协议":
        Utils.jump2WebView(context, "互联网注册协议",
            App.PATH_REMOTE_H5 + "/person/agreementRegister.html");
        break;
      case "隐私政策":
        Utils.jump2WebView(context, "法律声明及隐私权政策",
            App.PATH_REMOTE_H5 + "/person/statementRegister.html");
        break;
      default:
    }
  }

  @override
  SettingBloc generateBloc() {
    return SettingBloc();
  }
}
